package com.bupt.server.obj;

import com.bupt.server.entity.Record;
import com.bupt.server.entity.Room;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

@Component
public class ACManager {
    public static int currtimer = 0;

    @PostConstruct
    public void ReadConfigText(){
        try {
            Schedule schedule = new Schedule();
            ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
            Runnable task = new Runnable() {
                @Override
                public void run() {
                        currtimer++;
                        schedule.ac_schedule_test();
                        schedule.CalServiceRecord(currtimer);
                        schedule.CalTemperature();
                        schedule.IsHuiWen();
                }
            };
            // 使用executor的scheduleAtFixedRate方法来每秒运行一次任务
            ScheduledFuture<?> future = executor.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void parser(String instruction) {
        String arg0Text = instruction.split(";")[0];
        String arg1Text = instruction.split(";")[1];
        Room room;
        int roomId = Integer.parseInt(arg0Text);
        synchronized (PublicData.class) {
            room = PublicData.totalRoomList.get(roomId);
        }

        if (arg1Text.contains(",")) {
            String arg10 = arg1Text.split(",")[0];
            String arg11 = arg1Text.split(",")[1];
            room.setTarget_temperature(Float.parseFloat(arg10));
            room.setSpeed(arg11);
        } else {
            switch (arg1Text) {
                case "on" -> {
                    room.setAc_status(arg1Text);
                    room.setSpeed("medium");
                    room.setTarget_temperature(22.0f);
                }
                case "off" -> {
                    room.setAc_status(arg1Text);
                }
                case "low", "medium", "high" -> {
                    room.setSpeed(arg1Text);
                }
                default -> {
                    room.setTarget_temperature(Float.parseFloat(arg1Text));
                }
            }
        }
    }
    private void showAllRoomDetailedRecords() {
        for(Integer id : PublicData.totalRoomList.keySet()) {
            Room r = PublicData.totalRoomList.get(id);
            r.makeDetailedRecord();
            for (Record record : r.recordList) {
                record.printRecord();
            }
        }
    }




}
